<?php
/**
 * Created by PhpStorm.
 * User: tiemeng
 * Date: 16/1/13
 * Time: 09:13
 */
class xunSearchModel{
    private $_iniPath='';
    private $_xs;
    private $_search;
    private $_name;
    public function __construct(){
        include_once FILE . 'lib/XS.class.php';
        $this->_iniPath = FILE."lib/xunsearch/";
    }

    public function init($config){
        $this->_xs = new XS($this->_iniPath.$config.".ini");
        $this->_search = $this->_xs->search;
        $this->_name = $config;
        return $this;
    }

    /**
     * 搜索结果集
     * @param $keywords
     * @param string $sortFields
     * @param int $limit
     * @param int $page
     * @return array
     */
    public function search($keywords,$sortFields='id',$limit=20,$page=1){
        if($page == 1){
            $offset = 0;
        }else{
            $limit = 10;
            $offset = $page*$limit;
        }
        $docs = $this->result($keywords,$sortFields,$limit,$offset);
        $redata = array();
        if($this->_name=="group"){ //搜索群组
            foreach($docs as $k=>$doc){
                $redata[$k]['gid'] = $doc->gid;
            }
        }
        elseif($this->_name=="activity"){ //搜索群组
            foreach($docs as $k=>$doc){
                $redata[$k]['aid'] = $doc->aid;
            }
        }
        else{
            $redata = $docs;
        }
        return $redata;
    }

    /**
     * 获取查询结果
     * @param $keywords
     * @param $sortFields
     * @param $limit
     * @param int $offset
     * @return mixed
     */
    public function result($keywords,$sortFields,$limit,$offset=0){
        $this->_search->setQuery($keywords)->setSort($sortFields)->setLimit($limit,$offset);
        return $this->_search->search();
    }

    /**
     * 更新索引
     * @param $data
     * @return mixed
     */
    public function updateIndex($data){
        if(is_array($data) && !empty($data)){
            $document = new XSDocument();
            $document->setFields($data);
            return $this->_xs->index->update($document);
        }
        return false;
    }

    /**
     * 增加索引
     * @param $data
     * @return mixed
     */
    public function addIndex($data){
        if(is_array($data) && !empty($data)){
            $document = new XSDocument();
            $document->setFields($data);
            return $this->_xs->index->add($document);
        }
        return false;

    }

    /**
     * @param $value
     * @param $field
     * @return mixed
     */
    public function delIndex($value,$field){
        return $this->_xs->index->del($value,$field);
    }

    /**
     * 平滑重建索引，适用于配置文件字段的修改或数据更改
     * @param $data 二维数组，包括配置文件中的字段
     */
    public function rebuildIndex($data){
        $this->_xs->index->stopRebuild();
        $this->_xs->index->beginRebuild();
        if(is_array($data) && !empty($data)){
            foreach($data as $info){
               $this->addIndex($info);
            }
        }
        $this->_xs->index->endRebuild();
    }

    function cl(){
        $this->_xs->index->clean();
    }

    public function getCount($keywords){
        return $this->_search->count($keywords);
    }

    public function getHotQuery($limit=6, $type='total'){
        return $this->_search->getHotQuery($limit,$type);
    }

    public function getLastCount(){
        return $this->_search->getLastCount();
    }

    public function getSearcher(){
        return $this->_search;
    }
    
    public function getIndex(){
        return $this->_xs->index;
    }

    //刷新索引，使其立即生效
    public function __destruct(){
        $this->_xs->index->flushindex();
    }

}